UHD Overview

Table of Contents

Official site

Codes Analysis

include/utils/static.hpp and lib/utils/static.cpp

lib/utils/static.cpp

_uhd_static_fixture::_uhd_static_fixture(void (*fcn)(void), const char *name){
    try{
        fcn();
    }
    catch(const std::exception &e){
        std::cerr << "Exception in static block " << name << std::endl;
        std::cerr << "  " << e.what() << std::endl;
    }
    catch(...){
        std::cerr << "Exception in static block " << name << std::endl;
    }
}

include/utils/static.hpp

/*!
 * Defines a function that implements the "construct on first use" idiom
 * \param _t the type definition for the instance
 * \param _x the name of the defined function
 * \return a reference to the lazy instance
 */
//定义一个staitc的函数,函数的返回类型是_t
#define UHD_SINGLETON_FCN(_t, _x) static _t &_x(){static _t _x; return _x;}  

/*!
 * Defines a static code block that will be called before main()
 * The static block will catch and print exceptions to std error.
 * \param _x the unique name of the fixture (unique per source)
 */
#define UHD_STATIC_BLOCK(_x) \
  void _x(void); \    //声明函数
static _uhd_static_fixture _x##_fixture(&_x, #_x);      \        //struct 初始化,直接调用函数_x
void _x(void) //函数的实现

//! Helper for static block, constructor calls function
struct UHD_API _uhd_static_fixture{
    _uhd_static_fixture(void (*)(void), const char *);
};

include/utils/paths.hpp and lib/utils/paths.cpp

include/utils/paths.hpp

namespace uhd{

    //! Get a string representing the system's temporary directory
    UHD_API std::string get_tmp_path(void);

    //! Get a string representing the system's appdata directory
    UHD_API std::string get_app_path(void);

    //! Get a string representing the system's pkg data directory
    UHD_API std::string get_pkg_data_path(void);

} //namespace uhd

lib/utils/paths.cpp

namespace fs = boost::filesystem;

/***********************************************************************
 * Determine the paths separator
 **********************************************************************/
#ifdef UHD_PLATFORM_WIN32
    static const std::string env_path_sep = ";";
#else
    static const std::string env_path_sep = ":";
#endif /*UHD_PLATFORM_WIN32*/

#define path_tokenizer(inp) \
    boost::tokenizer<boost::char_separator<char> > \
    (inp, boost::char_separator<char>(env_path_sep.c_str()))

/***********************************************************************
 * Get a list of paths for an environment variable
 **********************************************************************/
static std::string get_env_var(const std::string &var_name, const std::string &def_val = ""){
    const char *var_value_ptr = std::getenv(var_name.c_str());
    return (var_value_ptr == NULL)? def_val : var_value_ptr;
}

static std::vector<fs::path> get_env_paths(const std::string &var_name){

    std::string var_value = get_env_var(var_name);

    //convert to filesystem path, filter blank paths
    std::vector<fs::path> paths;
    if (var_value.empty()) return paths; //FIXME boost tokenizer throws w/ blank strings on some platforms
    BOOST_FOREACH(const std::string &path_string, path_tokenizer(var_value)){
        if (path_string.empty()) continue;
        paths.push_back(fs::system_complete(path_string));
    }
    return paths;
}

/***********************************************************************
 * Implement the functions in paths.hpp
 **********************************************************************/
std::string uhd::get_tmp_path(void){
    const char *tmp_path = NULL;

    //try the official uhd temp path environment variable
    tmp_path = std::getenv("UHD_TEMP_PATH");
    if (tmp_path != NULL) return tmp_path;

    //try the windows function if available
    #ifdef USE_GET_TEMP_PATH
    char lpBuffer[2048];
    if (GetTempPath(sizeof(lpBuffer), lpBuffer)) return lpBuffer;
    #endif

    //try windows environment variables
    tmp_path = std::getenv("TMP");
    if (tmp_path != NULL) return tmp_path;

    tmp_path = std::getenv("TEMP");
    if (tmp_path != NULL) return tmp_path;

    //try the stdio define if available
    #ifdef P_tmpdir
    if (P_tmpdir != NULL) return P_tmpdir;
    #endif

    //try unix environment variables
    tmp_path = std::getenv("TMPDIR");
    if (tmp_path != NULL) return tmp_path;

    //give up and use the unix default
    return "/tmp";
}

usrp/subdev_spec

struct UHD_API subdev_spec_pair_t : boost::equality_comparable<subdev_spec_pair_t>{
    //! The daughterboard slot name
    std::string db_name;

    //! The subdevice name
    std::string sd_name;
class UHD_API subdev_spec_t : public std::vector<subdev_spec_pair_t>

usrp/dboard_manager.cpp

//dboard registry tuple: dboard constructor, canonical name, subdev names
typedef boost::tuple<dboard_manager::dboard_ctor_t, std::string, prop_names_t> args_t;

//map a dboard id to a dboard constructor
typedef uhd::dict<dboard_key_t, args_t> id_to_args_map_t;
UHD_SINGLETON_FCN(id_to_args_map_t, get_id_to_args_map)

main member:

uhd::dict<std::string, subdev_proxy::sptr> _rx_dboards;

        _rx_dboards[subdev] = subdev_proxy::sptr(
            new subdev_proxy(xcvr_dboard, subdev_proxy::RX_TYPE)
        );

uhd::dict<std::string, subdev_proxy::sptr> _tx_dboards;
        _tx_dboards[subdev] = subdev_proxy::sptr(
            new subdev_proxy(xcvr_dboard, subdev_proxy::TX_TYPE)
        );

dboard_iface::sptr _iface;  ==>_dboard_iface  (_fpga_ctrl, _fpga_i2c_ctrl....)

usrp/e100/e100_mmap_zero_copy.cpp

//re-usable managed buffers
std::vector<e100_mmap_zero_copy_mrb> _mrb_pool;
std::vector<e100_mmap_zero_copy_msb> _msb_pool;

    //initialize the managed receive buffers
    for (size_t i = 0; i < get_num_recv_frames(); i++){
        _mrb_pool.push_back(e100_mmap_zero_copy_mrb(
            recv_buff + get_recv_frame_size()*i, (*recv_info) + i
        ));
    }

    //initialize the managed send buffers
    for (size_t i = 0; i < get_num_recv_frames(); i++){
        _msb_pool.push_back(e100_mmap_zero_copy_msb(
            send_buff + get_send_frame_size()*i, (*send_info) + i,
            get_send_frame_size(), _fd
        ));
    }

Author: Shi Shougang

Created: 2015-03-05 Thu 23:21

Emacs 24.3.1 (Org mode 8.2.10)

Validate